package com.service;

import com.dao.OrderItemMapper;
import com.dao.OrderMapper;
import com.pojo.Order;
import com.pojo.OrderItem;
import com.pojo.OrderStatus;
import com.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


@Transactional
@Service
public class StatisticsService {
    //统计 服务层
    @Autowired
    private  OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    //获取最近一周内的日期,格式是年月日
    public List<String> getCurrentWeekDays(){
        List<String> currentWeekDays = new ArrayList<>();

        for(int day=0; day<7; day++){
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -day);   //日期从今天往前推
            Date date = calendar.getTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String dateStr = simpleDateFormat.format(date);
            currentWeekDays.add(dateStr);
        }
        Collections.reverse(currentWeekDays);

        return currentWeekDays;

    }

    //统计各种食品类型的点餐（已付款）的点餐数量, 传入食品类型id
    public Integer getFoodTypeStat(Integer dtId){
        Integer sum = 0;

        //已付款的订单
        List<Order> orderList = orderMapper.selectAllByOrderStatus(OrderStatus.PAID.getStatus());

        if(orderList!=null){
            for(int i=0; i< orderList.size(); i++){
                List<OrderItem> orderItemList = orderList.get(i).getOrderItems();  //获取指定订单号的订单项
                if(orderItemList!=null){
                    //订单项中的是否有这种类型的菜，统计
                    for(OrderItem orderItem: orderItemList) {
                        if(orderItem.getDish().getDtId() == dtId){
                            sum += orderItem.getNum();
                        }
                    }
                }
            }
            return sum;
        }

        return null;

    }


    //统计指定某一天的营业额(只选取已经付款的订单)
    public BigDecimal getDailyTotalStat(String date){
        List<Order> orderList = orderMapper.selectOrderByDate(date);
        BigDecimal dailyTotalMoney = new BigDecimal("0");  //日营业额
        if(orderList!=null){
            for(Order order: orderList){
                if(order.getoTotalMoney()!=null){
                    dailyTotalMoney = dailyTotalMoney.add(order.getoTotalMoney());
                }
            }
            return dailyTotalMoney;
        }
        return null;
    }


    //返回干净的日期列表
    public List getCleanDateList(List<String> list){
        List<String> newList = new ArrayList<>();
        for(String date: list){
            String newDate = DateUtil.parseTime(date);
            newList.add(newDate);
        }
        return newList;
    }


    //查询带条件的订单数据
    public List<Order> selectOrderByDateAndDishType(Integer dtId, List<String> dateRange){
//        List<String> dateList = map.get("dateRange");
//        List<String> dishtypeList = map.get("dishTypeSelect");
//        if(dtId != null)

        Map<String, Object> map = new HashMap<>();
        map.put("dishtypeId", dtId);
        map.put("dateRangeFront", dateRange.get(0));
        map.put("dateRangeEnd",dateRange.get(1));
        map.put("oStatus", OrderStatus.PAID.getStatus());

        List<Order> orderList =  orderMapper.selectOrderByDateAndDishType(map);

        return orderList;
    }


    //一段时间内的订单数据,此方法优化需要和上一个方法合并
    public List<Order> selectOrderByDateRange(List<String> dateRange){
        if(dateRange!=null){
            Map<String,Object> map = new HashMap<>();
            map.put("dateRangeFront",dateRange.get(0));
            map.put("dateRangeEnd", dateRange.get(1));
            map.put("oStatus", OrderStatus.PAID.getStatus());

            List<Order> orderList = orderMapper.selectOrderByDateRange(map);
            return orderList;
        }

        return null;
    }


    //一段时间以内总营业额,前提是订单列表中有
    public Map<String, ? extends Object> getDateRangeTotalMoney(List<Order> orderList){
        if(orderList!=null){
            Map<String, BigDecimal> map = new TreeMap<>();
            for(Order order: orderList){
                //为空
                if(map.get(order.getoPayoffDate().toString()) != null){
                    map.put(order.getoPayoffDate().toString(), order.getoTotalMoney());
                }else{
                    if(map.get(order.getoPayoffDate().toString())!=null && order.getoTotalMoney()!=null){
                        BigDecimal total = map.get(order.getoPayoffDate().toString()).add(order.getoTotalMoney());
                        map.put(order.getoPayoffDate().toString(), total);
                    }

                }
            }
            return map;

        }
        return null;
    }




    //JAVA获取某段时间内的所有日期
    public List<Date> findDates(Date dBegin, Date dEnd) {
        List dateList = new ArrayList();
        dateList.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            dateList.add(calBegin.getTime());
        }
        return dateList;
    }


    public BigDecimal getDateRangeTotalByDishTyepId(List<Order> orderList){
        if(orderList!=null){
            BigDecimal subTotal = new BigDecimal("0");
            for(Order order: orderList){
                List<OrderItem> orderItemList = order.getOrderItems();
                if(orderItemList!=null){
                    for(OrderItem orderItem: orderItemList){
                        subTotal = subTotal.add(orderItem.getOiSubtotal());
                    }
                }
            }
            return subTotal;
        }

        return null;

    }














}
